﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Threading;
using Microsoft.MediaCenter.UI;
using Level65.ClipShow;
using Level65.WMVHelper;

namespace ClipShow
{
    public class MediaListItem : ModelItem
    {
        private MediaMetaData _sourceItem;
        public MediaListItem()
        {

        }

        public MediaListItem(MediaMetaData source)
        {
            _sourceItem = source;
        }

        public MediaListItem(IModelItemOwner owner, int index, ArrayListDataSet source) : base(owner)
        {
            _sourceItem = GetMediaItem(index, source);
        }

        public MediaMetaData SourceItem
        {
            get { return (_sourceItem); }
            set { _sourceItem = value;  }
        }
        public MediaMetaData MmdSourceItem
        {
            get { return (_sourceItem.Clone());}
        }
        public Image Thumbnail
        {
            get { return (_sourceItem.Thumbnail); }
        }
        protected override void Dispose(bool disposing)
        {
           base.Dispose(disposing);
           if (_sourceItem != null)
           {
               if (_sourceItem.Thumbnail != null)
               {
                   try
                   {
                       _sourceItem.Thumbnail.Dispose();
                       _sourceItem.Thumbnail = null;
                   }
                   catch { }
               }
           }
        }
        
        private static MediaMetaData GetMediaItem(int Index, ArrayListDataSet source)
        {
            MediaMetaData result = (MediaMetaData)source[Index];
            if (result.Thumbnail == null)
            {
                result.Thumbnail = new Image(BuildImagePath(result.ThumbnailUrl));
            }
            return (result);
        }

        internal static string BuildImagePath(string sourceUrl)
        {
            if (sourceUrl.StartsWith("http"))
            {
                return (sourceUrl);
            }
            else
            {
                if (File.Exists(sourceUrl))
                {
                    return (String.Format("file://{0}", sourceUrl));
                }
                else
                {
                    return ("resx://ClipShow/ClipShow.Resources/clipShow_glyph");
                }
            }
        }
    }
    public class PreviewChannelItemList : VirtualList
    {
        private ArrayListDataSet _sourceList;
        public PreviewChannelItemList()
        {
            VisualReleaseBehavior = ReleaseBehavior.Dispose;
            EnableSlowDataRequests = false;
        }

        public ArrayListDataSet Source
        {
            set
            {
                _sourceList = value;
                Count = _sourceList.Count;
            }
        }

        protected override void OnRequestItem(int index, ItemRequestCallback callback)
        {
            PreviewChannelVItem mli = new PreviewChannelVItem(this, index, _sourceList);
            callback(this, index, mli);
        }

    }
    public class PreviewChannelVItem : ModelItem
    {
        private PreviewChannelItem _sourceItem;
        public PreviewChannelVItem()
        {

        }
        public PreviewChannelVItem(PreviewChannelItem source)
        {
            _sourceItem = source;
            //_sourceItem.Thumbnail = new Image(BuildImagePath(source.ThumbnailUrl));
        }

        public PreviewChannelVItem(IModelItemOwner owner, int index, ArrayListDataSet source)
            : base(owner)
        {
            _sourceItem = GetPCI(index, source);
        }

        public PreviewChannelItem SourceItem
        {
            get { return (_sourceItem); }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (_sourceItem != null)
            {
                if (_sourceItem.Thumbnail != null)
                {
                    try
                    {
                        _sourceItem.Thumbnail.Dispose();
                        _sourceItem.Thumbnail = null;
                    }
                    catch { }
                }
            }
        }

        private static PreviewChannelItem GetPCI(int Index, ArrayListDataSet source)
        {
            PreviewChannelItem result = (PreviewChannelItem)source[Index];
            if (result.ThumbnailUrl.Length > 0)
            {
                if (result.Thumbnail ==null)
                {
                    result.Thumbnail = new Image(BuildImagePath(result.ThumbnailUrl));
                }
            }
            return (result);
        }

        internal static string BuildImagePath(string sourceUrl)
        {
            if (sourceUrl.StartsWith("http"))
            {
                return (sourceUrl);
            }
            else
            {
                if (File.Exists(sourceUrl))
                {
                    return (String.Format("file://{0}", sourceUrl));
                }
                else
                {
                    return ("resx://ClipShow/ClipShow.Resources/clipShow_glyph");
                }
            }
        }
    }
    public class MediaList : VirtualList
    {
        public MediaList()
        {
            VisualReleaseBehavior = ReleaseBehavior.KeepReference;
            EnableSlowDataRequests = false;
        }
    }
}
